package com.ruoyi.classai.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.classai.domain.*;
import com.ruoyi.classai.domain.vo.ClassListVo;
import com.ruoyi.classai.mapper.EwcClassMapper;
import com.ruoyi.classai.service.IEwcClassCourseService;
import com.ruoyi.classai.service.IEwcClassService;
import com.ruoyi.classai.service.IEwcClassStudentService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.exception.GlobalException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 班级信息Service业务层处理
 * 
 * @author lcj
 * @date 2025-02-27
 */
@Service
public class EwcClassServiceImpl extends ServiceImpl<EwcClassMapper, EwcClass> implements IEwcClassService {

    @Autowired
    private IEwcClassStudentService classStudentService;
    @Autowired
    private IEwcClassCourseService classCourseService;

    @Override
    public void addClass(EwcClass ewcClass) {
        ewcClass.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
        this.save(ewcClass);
    }

    @Override
    public ClassListVo getClassById(String classId) {
        EwcClass ewcClass=this.getById(classId);
        ClassListVo classListVo=new ClassListVo();
        //查询课程
        long courseNum=classCourseService.countClassCourse(classId,ewcClass.getTeacherId());
        //查询学生
        long studentNum=classStudentService.count(Wrappers.<EwcClassStudent>lambdaQuery()
                .eq(EwcClassStudent::getClassId,ewcClass.getId()));
        classListVo.setCourseNum(courseNum);
        classListVo.setStudentNum(studentNum);
        classListVo.setId(ewcClass.getId());
        classListVo.setClassDescription(ewcClass.getClassDescription());
        classListVo.setClassName(ewcClass.getClassName());
        classListVo.setClassType(ewcClass.getClassType());
        classListVo.setClassPhotoPath(ewcClass.getClassPhotoPath());
        classListVo.setDeptId(ewcClass.getDeptId());
        classListVo.setClassGrade(ewcClass.getClassGrade());
        return classListVo;
    }

    @Override
    public List<ClassListVo> getClassList(String className, Integer classType) {
        List<EwcClass> list=this.list(Wrappers.<EwcClass>lambdaQuery().eq(EwcClass::getStatus, Constants.COMMON_SUCCESS)
                        .eq(EwcClass::getTeacherId,ThirdSessionHolder.getThirdSession().getId())
                .eq(StringUtils.isNotBlank(className),EwcClass::getClassName,className)
                .eq(Objects.nonNull(classType),EwcClass::getClassType,classType));
        List<ClassListVo> resList=new ArrayList<>();
        for (EwcClass ewcClass : list) {
            ClassListVo classListVo=new ClassListVo();
            //查询课程
            long courseNum=classCourseService.countClassCourse(ewcClass.getId(),ewcClass.getTeacherId());
            //查询学生
            long studentNum=classStudentService.count(Wrappers.<EwcClassStudent>lambdaQuery().eq(EwcClassStudent::getClassId,ewcClass.getId()));
            classListVo.setCourseNum(courseNum);
            classListVo.setStudentNum(studentNum);
            classListVo.setId(ewcClass.getId());
            classListVo.setClassDescription(ewcClass.getClassDescription());
            classListVo.setClassName(ewcClass.getClassName());
            classListVo.setClassType(ewcClass.getClassType());
            classListVo.setClassPhotoPath(ewcClass.getClassPhotoPath());
            classListVo.setDeptId(ewcClass.getDeptId());
            resList.add(classListVo);
        }
        return resList;
    }

    @Override
    public void deleteClassCourse(String id) {
        //查询是否有班级和学生
        //查询课程
        long courseNum=classCourseService.count(Wrappers.<EwcClassCourse>lambdaQuery().eq(EwcClassCourse::getClassId,id));
        if (courseNum>0){
            throw new GlobalException("已绑定课程,不能删除");
        }
        //查询学生
        long studentNum=classStudentService.count(Wrappers.<EwcClassStudent>lambdaQuery().eq(EwcClassStudent::getClassId,id));
        if (studentNum>0){
            throw new GlobalException("已绑定学生,不能删除");
        }
        //删除课程
        this.removeById(id);
    }

    @Override
    public ClassListVo getGroupClassInfo(String classId) {
        EwcClass ewcClass=this.getById(classId);
        //查询学生
        long studentNum=classStudentService.count(Wrappers.<EwcClassStudent>lambdaQuery().eq(EwcClassStudent::getClassId,ewcClass.getId()));
        ClassListVo classListVo=new ClassListVo();
        classListVo.setClassName(ewcClass.getClassName());
        classListVo.setClassDescription(ewcClass.getClassDescription());
        classListVo.setId(classId);
        classListVo.setStudentNum(studentNum);
        return classListVo;
    }
}
